// 节流函数
export function throttle(fn: Function, wait: number = 200) {
  let flag = false;
  return function (this: Function) {
    if (flag) return;
    flag = true;
    setTimeout(() => {
      flag = false;
      return fn.apply(this, arguments);
    }, wait);
  };
}
// export function throttle(fn: Function, wait: number = 200) {
//   let preDate = Date.now();
//   return function (this: unknown) {
//     const nowDate = Date.now();
//     if (nowDate >= preDate + wait) {
//       preDate = nowDate;
//       return fn.apply(this, arguments);
//     }
//   };
// }

// 防抖函数
export function debounce(fn: Function, delay: number = 200) {
  let timer = 0;
  return function (this: unknown) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      return fn.apply(this, arguments);
    }, delay);
  };
}
